package com.ruoyi.common.core.domain;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.filter.PropertyPreFilter;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableField;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.ruoyi.common.annotation.Excel;
import com.ruoyi.common.core.domain.tree.ITree;
import com.ruoyi.common.utils.SecurityUtils;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import org.apache.commons.lang3.ObjectUtils;

import java.io.Serializable;
import java.util.*;

/**
 * Entity基类
 * 
 * @author ruoyi
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class BaseEntity implements Serializable {

    /** 搜索值 */
    @JsonIgnore
    @TableField(exist = false)
    private String searchValue;

    /** 备注 */
    @Excel(name = "备注")
    private String remark;

    /** 创建者 */
    @TableField(fill = FieldFill.INSERT)
    private String createBy;

    /** 创建时间 */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @Excel(name = "创建时间", width = 30, dateFormat = "yyyy-MM-dd HH:mm:ss")
    @TableField(fill = FieldFill.INSERT)
    private Date createTime;

    /** 更新者 */
    @TableField(fill = FieldFill.UPDATE)
    private String updateBy;

    /** 更新时间 */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @Excel(name = "更新时间", width = 30, dateFormat = "yyyy-MM-dd HH:mm:ss")
    @TableField(fill = FieldFill.UPDATE)
    private Date updateTime;

    /** 请求参数 */
    @JsonInclude(JsonInclude.Include.NON_EMPTY)
    @TableField(exist = false)
    private Map<String, Object> params;

    public Map<String, Object> getParams() {
        if (params == null)
        {
            params = new HashMap<>();
        }
        return params;
    }

    /**
     * 父类转成字类
     * @param <T>
     * @return
     */
    public <T extends BaseEntity> T toExtentEntity(Class<T> clz){
        return JSON.parseObject(JSON.toJSONString(this), clz);
    }

    /**
     * 转JsonObject
     * @return
     */
    public JSONObjectPro toJsonObject(){
        return new JSONObjectPro(JSON.parseObject(JSON.toJSONString(this)));
    }

    /**
     * 转仅包含这些字段的JsonObject
     */
    public JSONObjectPro includeJsonObject(String... names){
        if (ObjectUtils.isEmpty(names)) return toJsonObject();
        List<String> nameList = Arrays.asList(names);
        return new JSONObjectPro(JSON.parseObject(JSON.toJSONString(this,
                (PropertyPreFilter) (jsonWriter, source, name) -> nameList.contains(name))));
    }

    /**
     * 转不包含这些字段的JsonObject
     */
    public JSONObjectPro excludeJsonObject(String... names){
        if (ObjectUtils.isEmpty(names)) return toJsonObject();
        List<String> nameList = Arrays.asList(names);
        return new JSONObjectPro(JSON.parseObject(JSON.toJSONString(this,
                (PropertyPreFilter) (jsonWriter, source, name) -> !nameList.contains(name))));
    }
}
